<template>
  <div>
    <!-- 表格 -->
    <el-table :data="skuList" style="width: 100%" border>
      <el-table-column type="index" align="center" label="序号" width="width"> </el-table-column>
      <el-table-column prop="skuName" label="名称" width="150px"> </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="300px"> </el-table-column>
      <el-table-column label="默认图片" width="width">
        <template slot-scope="{ row }">
          <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(KG)" width="width"> </el-table-column>
      <el-table-column prop="price" label="价格(元)" width="width"> </el-table-column>
      <el-table-column label="操作" width="300px">
        <template slot-scope="{ row }">
          <el-button type="success" size="mini" icon="el-icon-sort-up" v-if="row.isSale == 1" @click="sortDown(row)"></el-button>
          <el-button type="info" size="mini" icon="el-icon-sort-down" v-else-if="row.isSale == 0" @click="sortUp(row)"></el-button>
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="edit"></el-button>
          <el-button type="info" size="mini" icon="el-icon-info" @click="showSkuDetail(row)"></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="$message({ type: 'info', message: '删除操作正在开发中...' })"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination :current-page="page" :page-sizes="[10, 15, 30]" :page-size="limit" layout="prev, pager, next, jumper,->,total,sizes" :total="total" style="text-align: center" @current-change="getData" @size-change="handleSizeChange"> </el-pagination>
    <!-- 抽屉 -->
    <el-drawer :visible.sync="show" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag type="success" v-for="skuAttr in skuInfo.skuAttrValueList" :key="skuAttr.id">{{ skuAttr.attrId }}--{{ skuAttr.valueId }}</el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="400px;width:200px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="" style="height: 100%; width: 100%" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 10,
      skuList: [],
      total: 0,
      show: false,
      skuInfo: {},
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    async getData(pages = 1) {
      this.page = pages
      let res = await this.$API.Sku.reqSkuList(this.page, this.limit)
      if (res.code == 200) {
        this.total = res.data.total
        this.skuList = res.data.records
      }
    },
    handleSizeChange() {
      this.limit = this.limit
      this.getData()
    },
    async sortDown(row) {
      row.isSale = 0
      let res = await this.$API.Sku.reqDownSale(row.id)
      if (res.code === 200) {
        this.$message({ type: 'success', message: '下架成功！' })
      }
    },
    async sortUp(row) {
      row.isSale = 1
      let res = await this.$API.Sku.reqUpSale(row.id)
      if (res.code === 200) {
        this.$message({ type: 'success', message: '上架成功！' })
      }
    },
    edit() {
      this.$message({ type: 'info', message: '编辑按钮正在开发中...' })
    },
    async showSkuDetail(row) {
      this.show = true
      let res = await this.$API.Sku.reqSkuById(row.id)
      if (res.code == 200) {
        this.skuInfo = res.data
      }
    },
  },
}
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-carousel__button {
  width: 10px;
  height: 10px;
  background: red;
  border-radius: 50%;
}
</style>

<style scoped>
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px 10px;
}
</style>